﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DevExpress.Xpf.Core;
using UIT.ScrumTool.Client.View.Project;
using UIT.ScrumTool.Client.ControlService;
using UIT.ScrumTool.Client.Controller;
using UIT.ScrumTool.Client.Utilities.Team;


namespace UIT.ScrumTool.Client.View.Team
{
    /// <summary>
    /// Interaction logic for dxwUserList.xaml
    /// </summary>
    public partial class dxwUserList : DXWindow
    {
        // Cache for User List
        public static List<UserDto> lstUser = null;
        public dxwUserList()
        {
            InitializeComponent();
            if (lstUser == null)
            {
                lstUser = UserCtrl.UserGetByAll();
            }
            this.grdUserList.ItemsSource = lstUser;
        }

        // Choose a Member in User List
        private void grdUserList_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            MemberInfo memberInfo = new MemberInfo();
            UserDto userDto = grdUserList.GetFocusedRow() as UserDto;
            ProjectUserDto projectUserDto = new ProjectUserDto();
            projectUserDto.ProjectId = uctrlProject.projectId;
            projectUserDto.UserId = userDto.Id;
            projectUserDto.UserRole = 2;

            // Insert Project User to ProjectUser Table
            int projectUserId = ProjectUserCtrl.ProjectUserInsert(projectUserDto);
            if (projectUserId != -1)
            {
                memberInfo = MemberInfo.CreateMemberInfo(userDto, uctrlProject.projectId, 2, projectUserId);
                if (memberInfo != null)
                {
                    lstUser.RemoveAll(u => u.Id == userDto.Id);
                    uctrlProject.UpdateMemberList(uctrlProject._listViewMember, memberInfo);
                }
            }

            this.grdUserList.ItemsSource = lstUser;
            this.grdUserList.RefreshData();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
